package com.example.DialogNumber;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.mattrigle.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomNumberPicker extends FrameLayout {

	private LayoutInflater inflater = null;
	private View view;
	private ImageView btnUp;
	private ImageView btnDown;
	private TextView txtValue;
	private int maxValue = 10;
	private int minValue = 0;
	private int value;
	private TextView txtDot;
	private ArrayList<DefaultMaxMin> defaultMaxMinValue = new ArrayList<DefaultMaxMin>();
	private ArrayList<CustomNumberPicker> numberPicker = new ArrayList<CustomNumberPicker>();

	public CustomNumberPicker(Context context) {
		super(context);
		setLayout(context);
	}

	public CustomNumberPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setLayout(context);
	}

	public CustomNumberPicker(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setLayout(context);
	}

	private void setLayout(Context context) {
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.layout_numberpicker, null);
		btnUp = (ImageView) view.findViewById(R.id.btnUp);
		btnDown = (ImageView) view.findViewById(R.id.btnDown);
		txtValue = (TextView) view.findViewById(R.id.txtValue);
		txtDot = (TextView) view.findViewById(R.id.txtDot);
		txtValue.setText("" + minValue);
		btnUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buttonUp();
			}
		});

		btnDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buttonDonw();
			}
		});
		this.addView(view);
	}

	private void buttonUp() {
		if (value < maxValue) {
			value++;
		}
		if (value > maxValue) {
			value = maxValue;
		}
		if(value == maxValue){
			Log.w("opo", "numberPicker.size "+numberPicker.size());
			if (numberPicker.size() > 0) {
				for (int i = 0; i < numberPicker.size(); i++) {
					numberPicker.get(i).setValue(0);
					numberPicker.get(i).setMaxMinValue(0, 0);
				}
			}
		}
		setTextValue(value);
	}

	private void buttonDonw() {
		if (value > minValue) {
			value--;
		}
		if (value <= minValue) {
			value = minValue;
		}

		if (numberPicker.size() > 0) {
			if (value < maxValue) {
				for (int i = 0; i < numberPicker.size(); i++) {
//					numberPicker.get(i).setValue(0);
					numberPicker.get(i).setMaxMinValue(defaultMaxMinValue.get(i).maxmin[0], defaultMaxMinValue.get(i).maxmin[1]);
				}

			}
		}
		setTextValue(value);
	}

	public void setMaxMinValue(int maxValue, int minValue) {
		this.maxValue = maxValue;
		this.minValue = minValue;
	}

	private void setTextValue(int value) {
		txtValue.setText("" + value);
	}

	public int[] getMaxMinValue() {
		int[] value = new int[2];
		value[0] = this.maxValue;
		value[1] = this.minValue;
		return value;
	}

	public int getValue() {
		return this.value;
	}
    public void setValue(int value){
    	this.value = value;
    	setTextValue(value);
    }
	public void setVisibilityDot(boolean visibility) {
		if (visibility) {
			this.txtDot.setVisibility(View.VISIBLE);
		} else {
			this.txtDot.setVisibility(View.GONE);
		}

	}

	public void limitePerValue(ArrayList<CustomNumberPicker> numberPicker) {
		this.numberPicker = numberPicker;

		for (int i = 0; i < numberPicker.size(); i++) {
			DefaultMaxMin defaultMaxMin = new DefaultMaxMin();
			defaultMaxMin.maxmin = numberPicker.get(i).getMaxMinValue();
			defaultMaxMinValue.add(defaultMaxMin);
		}
	}
}
